package com.jy.modules.cims.component.loan;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.CimsIrtUrlConstant;
import com.jy.modules.cims.common.JResult;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.component.common.service.TCMmCooperorgDealService;
import com.jy.modules.cims.component.common.service.TCMmCreditDetailService;
import com.jy.modules.cims.component.common.service.TCMmCreditPackageService;
import com.jy.modules.cims.component.common.service.TCMmLoanDetailService;
import com.jy.modules.cims.component.common.service.TCMmRaisePlanService;
import com.jy.modules.cims.component.common.service.TCMmTbjLoanProdService;
import com.jy.modules.cims.data.common.dto.TCMmCooperorgDealDTO;
import com.jy.modules.cims.data.common.dto.TCMmCreditDetailDTO;
import com.jy.modules.cims.data.common.dto.TCMmCreditPackageDTO;
import com.jy.modules.cims.data.common.dto.TCMmLoanDetailDTO;
import com.jy.modules.cims.data.common.dto.TCMmRaisePlanDTO;
import com.jy.modules.cims.data.common.dto.TCMmTbjLoanProdDTO;
import com.jy.modules.cims.data.dto.ConfirmParamDto;
import com.jy.modules.cims.data.dto.ConfirmParamDto.LoanApproveInfo;
import com.jy.modules.cims.data.dto.inter.tbj.fk.LoanResultDto;
import com.jy.modules.cims.data.dto.inter.tbj.sh.AuditResultDto;
import com.jy.modules.cims.data.dto.loan.LoanFundOutDto;
import com.jy.modules.cims.data.dto.loan.LoanFundOutDto.LoanFundOutInfo;
import com.jy.modules.cims.data.interact.tbj.loan.response.ResponseDataEnum;
import com.jy.modules.cims.interact.ICInteract;

@Service("com.jy.modules.cims.component.loan.TBJCLoanImpl")
public class TBJCLoanImpl {
	private static final Logger logger = LoggerFactory.getLogger(TBJCLoanImpl.class);

	@Autowired
	@Qualifier("com.jy.modules.cims.interact.CCInteractImpl")
	private ICInteract iCInteract;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmRaisePlanService")
	private TCMmRaisePlanService tcmmRaisePlanService;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmTbjLoanProdService")
	private TCMmTbjLoanProdService tcmmTbjLoanProdService;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmLoanDetailService")
	private TCMmLoanDetailService tcmmLoanDetailService;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmCreditDetailService")
	private TCMmCreditDetailService tcmmCreditDetailService;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmCooperorgDealService")
	private TCMmCooperorgDealService tcmmCooperorgDealService;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmRaisePlanService")
	private TCMmRaisePlanService tCMmRaisePlanService;
	 
	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmCreditPackageService")
	private TCMmCreditPackageService creditPackageService;

	/**
	 * @description 登记募集计划申请
	 * @param tcmmRaisePlanDTO
	 *            募集计划申请实体对象
	 * @return 主键id
	 * @throws Exception
	 * @author shengzhoutao
	 * @date 2015-09-07
	 */
	public long registerRaisePlan(final TCMmRaisePlanDTO tcmmRaisePlanDTO) throws Exception {
		TCMmRaisePlanDTO newRaisePlan = tcmmRaisePlanDTO;
		newRaisePlan.setPlayType(CimsConstant.RAISE_TYPE_LOA);// 募集类型(01:债权;
																// 02:贷款)
		newRaisePlan.setCooperorgCode(CimsConstant.COOPERORG_CODE_TBJ);// 合作机构
		newRaisePlan.setCreateTime(new Timestamp(System.currentTimeMillis()));
		newRaisePlan.setModifyTime(new Timestamp(System.currentTimeMillis()));
		newRaisePlan.setRaiseStatus(CimsConstant.RAISE_STATUS_BEGIN);// 募集状态(01:募集中;
		newRaisePlan.setInitPeriod(tcmmRaisePlanDTO.getMerchantPeriod());//商户兑付期限															// 02:募集完成)
		newRaisePlan.setValidateState(CimsConstant.DATE_STATUS_VALID);
		return tcmmRaisePlanService.insertTCMmRaisePlan(newRaisePlan);
	}

	/**
	 * @description 登记产品相关的债权信息
	 * @param tcmmRaisePlanDTO
	 *            产品相关的债权信息
	 * @return 主键id
	 * @throws Exception
	 * @author shengzhoutao
	 * @date 2015-09-07
	 */
	public long registerTbjLoanProd(final TCMmTbjLoanProdDTO tcmmTbjLoanProdDTO) throws Exception {
		tcmmTbjLoanProdDTO.setCreateTime(new Timestamp(System.currentTimeMillis()));
		tcmmTbjLoanProdDTO.setModifyTime(new Timestamp(System.currentTimeMillis()));
		tcmmTbjLoanProdDTO.setValidateState(CimsConstant.DATE_STATUS_VALID);
		return tcmmTbjLoanProdService.insertTCMmTbjLoanProd(tcmmTbjLoanProdDTO);
	}

	/**
	 * 
	 * @Description 登记产品的上架时间
	 * @param tcmmTbjLoanProdDTO
	 * @return
	 * @throws Exception   
	 * long  
	 * @throws
	 * @author zhangmi
	 * @date 2016-6-29
	 */
	public long registerTbjProdInfo(final TCMmTbjLoanProdDTO tcmmTbjLoanProdDTO)throws Exception{
		tcmmTbjLoanProdDTO.setCreateTime(new Timestamp(System.currentTimeMillis()));
		tcmmTbjLoanProdDTO.setModifyTime(new Timestamp(System.currentTimeMillis()));
		tcmmTbjLoanProdDTO.setValidateState(CimsConstant.DATE_STATUS_VALID);
		tcmmTbjLoanProdDTO.setGroundDate(new Timestamp(System.currentTimeMillis()));//上架时间
		return tcmmTbjLoanProdService.insertTCMmTbjLoanProd(tcmmTbjLoanProdDTO);
	}
	
	/**
	 * @description updateLoanDetailProductCode
	 * @param contractNo
	 *            借款信息审批编号
	 * @param productCode
	 *            贷款产品代码
	 * @throws Exception
	 * @author shengzhoutao
	 * @date 2015-09-07
	 */
	public void updateLoanDetailProductCode(final String contractNo, final String productCode) throws Exception {
		TCMmLoanDetailDTO tcmmLoanDetail = new TCMmLoanDetailDTO();
		tcmmLoanDetail.setCooperorgCode(CimsConstant.COOPERORG_CODE_TBJ);
		tcmmLoanDetail.setContractNo(contractNo);
		List<TCMmLoanDetailDTO> tcmmLoanDetailList = tcmmLoanDetailService.searchTCMmLoanDetail(SearchParamUtil.getSearchParam(tcmmLoanDetail));

		tcmmLoanDetail = null;
		if (CollectionsUtils.isEmpty(tcmmLoanDetailList)) {
			return;
		}

		for (TCMmLoanDetailDTO dto : tcmmLoanDetailList) {
			dto.setProductCode(productCode);
			dto.setModifyTime(new Timestamp(System.currentTimeMillis()));
			tcmmLoanDetailService.updateTCMmLoanDetail(dto);
		}
	}

	/**
	 * @description 审批贷款: update T_C_MM_LOAN_DETAIL t1 set t1.LOAN_EACH_STATUS
	 *              ='${loanStatus}' , t1.LOAN_STATUS ='${loanDetailStatus}'
	 *              where t1.contract_no in ('${contractNos}')
	 * @param contractNo
	 *            借款信息审批编号
	 * @param loanStatus
	 *            贷款交互状态
	 * @param loanDetailStatus
	 *            贷款状态
	 * @return
	 * @throws Exception
	 * @author shengzhoutao
	 * @date 2015-09-07
	 */
	public boolean auditLoan(final String contractNo, final String loanStatus, final String loanDetailStatus,final Date fundDate) throws Exception {
		TCMmLoanDetailDTO tcmmLoanDetail = getLoanDetailByContractNo(contractNo);
		if (null == tcmmLoanDetail) {
			return false;
		}

		if (StringUtils.hasText(loanStatus)) {
			tcmmLoanDetail.setLoanEachStatus(loanStatus);
		}

		tcmmLoanDetail.setLoanStatus(loanDetailStatus);
		tcmmLoanDetail.setModifyTime(new Timestamp(System.currentTimeMillis()));
		if(null!=fundDate){
			tcmmLoanDetail.setFundDate(fundDate);
		}
		
		tcmmLoanDetailService.updateTCMmLoanDetail(tcmmLoanDetail);

		return true;
	}

	/**
	 * @description 调用贷款审批确认接口
	 * @param contractNo
	 *            借款信息审批编号
	 * @param loanStatus
	 * @throws Exception
	 */
	public void approveConfrim(final String contractNo, final String loanStatus) throws Exception {
		ConfirmParamDto approveDto = new ConfirmParamDto();
		List<ConfirmParamDto.LoanApproveInfo> approveList = new ArrayList<ConfirmParamDto.LoanApproveInfo>();

		LoanApproveInfo loanApproveInfo = approveDto.new LoanApproveInfo();
		loanApproveInfo.setApproveReason("");
		loanApproveInfo.setContractNo(contractNo);
		loanApproveInfo.setApproveType(loanStatus);
		approveList.add(loanApproveInfo);

		approveDto.setBusiType(CimsConstant.QUOTATYPE_LOAN);
		approveDto.setDealCode("");
		approveDto.setApproveList(approveList);

		// 调用贷款审批确认接口
		iCInteract.approveConfrim(approveDto);

		approveDto = null;
		approveList = null;
	}

	/**
	 * @description 调用贷款放款确认接口
	 * @param contractNo
	 *            借款信息审批编号
	 * @param loanStatus
	 * @throws Exception
	 */
	public void loanFundOut(final String contractNo, final String loanStatus,final Date fundDate) throws Exception {
		LoanFundOutDto loanFundOutDto = new LoanFundOutDto();
		List<LoanFundOutInfo> loanFundOutInfoList = new ArrayList<LoanFundOutInfo>();
		LoanFundOutInfo loanFundOutInfo = loanFundOutDto.new LoanFundOutInfo();
		loanFundOutInfo.setContractNo(contractNo);
		loanFundOutInfo.setOperReason("");
		loanFundOutInfo.setOperType(loanStatus);
		loanFundOutInfo.setPayDate(fundDate);//放款日期
		loanFundOutInfoList.add(loanFundOutInfo);

		loanFundOutDto.setDealCode("");
		loanFundOutDto.setLoanFundOutInfoList(loanFundOutInfoList);
		iCInteract.loanFundOut(loanFundOutDto);

		loanFundOutDto = null;
	}

	/***
	 * @description insertCreditByContractNo
	 * @param contractNo
	 * @param cooperorgDealDTO
	 * @throws Exception
	 */
	public void insertCreditByContractNo(String contractNo, Date fundDate) throws Exception {
		TCMmLoanDetailDTO tcmmLoanDetailDTO = getLoanDetailByContractNo(contractNo);
		
		TCMmCooperorgDealDTO tcmmCooperorgDealDTO = new TCMmCooperorgDealDTO();
		tcmmCooperorgDealDTO.setDealCode(tcmmLoanDetailDTO.getDealCode());
		List<TCMmCooperorgDealDTO> tcmmCooperorgDealList = tcmmCooperorgDealService.searchTCMmCooperorgDeal(SearchParamUtil
				.getSearchParam(tcmmCooperorgDealDTO));
		if (CollectionsUtils.isEmpty(tcmmCooperorgDealList)) {
			return;
		}

		tcmmCooperorgDealDTO = tcmmCooperorgDealList.get(0);
		TCMmCreditDetailDTO tcmmCreditDetailDTO = new TCMmCreditDetailDTO();
		tcmmCreditDetailDTO.setCreditCode(tcmmLoanDetailDTO.getContractNo());
		List<TCMmCreditDetailDTO> tcmmCreditDetailList = tcmmCreditDetailService.searchTCMmCreditDetail(SearchParamUtil
				.getSearchParam(tcmmCreditDetailDTO));
		if (CollectionsUtils.isNotEmpty(tcmmCreditDetailList)) {
			return;
		}
		
		//获取包信息
		Map<String, Object> searchParams = new HashMap<String, Object>();
		TCMmCreditPackageDTO tCMmCreditPackageDTO = new TCMmCreditPackageDTO();
		tCMmCreditPackageDTO.setContractCode(tcmmCooperorgDealDTO.getContractCode());
		tCMmCreditPackageDTO.setCooperorgCode(tcmmCooperorgDealDTO.getCooperorgCode());
		tCMmCreditPackageDTO.setCreditPackageName(tcmmCooperorgDealDTO.getCreditPackageName());
    	searchParams.put("dto", tCMmCreditPackageDTO);
		
		List<TCMmCreditPackageDTO> list = creditPackageService.searchTCMmCreditPackage(searchParams);
        

		tcmmCreditDetailDTO = new TCMmCreditDetailDTO();
		tcmmCreditDetailDTO.setCreditCode(tcmmLoanDetailDTO.getContractNo());
		tcmmCreditDetailDTO.setCreditSource(CimsConstant.CREDIT_SOURCE_LOA);
		tcmmCreditDetailDTO.setCooperorgCode(tcmmLoanDetailDTO.getCooperorgCode());
		tcmmCreditDetailDTO.setPackageNum(tcmmLoanDetailDTO.getCreditPackageName());
		tcmmCreditDetailDTO.setAgreementCode(tcmmCooperorgDealDTO.getAgreementCode());
		tcmmCreditDetailDTO.setContractCode(tcmmCooperorgDealDTO.getContractCode());
		tcmmCreditDetailDTO.setProductType(tcmmLoanDetailDTO.getProductType());
		tcmmCreditDetailDTO.setCreditorStatus(CimsConstant.CREDIT_STATUS_WIN);
		tcmmCreditDetailDTO.setRepayType(tcmmLoanDetailDTO.getRepayType());//插入债权信息没有插入还款方式 zhoup 2016-0701
		tcmmCreditDetailDTO.setCreditorValueUpdateDate(new java.util.Date());
		
		if(list.size()>0){
			tCMmCreditPackageDTO =  list.get(0);
			if(null != tCMmCreditPackageDTO.getPlanRedeemDate()){
				tcmmCreditDetailDTO.setPlanRedeemDate(tCMmCreditPackageDTO.getPlanRedeemDate());//计划赎回日期
			}
		}
		
		tcmmCreditDetailDTO.setPlanRedeemDate(tcmmLoanDetailDTO.getPlanRedeemDate());
		tcmmCreditDetailDTO.setDeliverDate(fundDate);
		tcmmCreditDetailDTO.setInitialLoanDate(fundDate);
		long key = tcmmCreditDetailService.insertTCMmCreditDetail(tcmmCreditDetailDTO);
		logger.info("insertTCMmCreditDetail的主键id:" + key);

		tcmmCooperorgDealDTO = null;
		tcmmCreditDetailDTO = null;
	}

	/**
	 * @description 验证贷款审批
	 * @param contractNo
	 *            借款信息审批编号
	 * @return JResult 错误信息
	 * @throws Exception
	 * @author shengzhoutao
	 * @date 2015-09-07
	 */
	public JResult validateAuditResultByContractNo(final String contractNo) throws Exception {
		JResult result = new JResult();
		result.setSuccess(false);
		result.setErrCode(String.valueOf(ResponseDataEnum.FAILURE.getCode()));
		TCMmLoanDetailDTO tcmmLoanDetail = getLoanDetailByContractNo(contractNo);
		if (null == tcmmLoanDetail) {
			result.setErrMsg("根据借款信息审批编号loanNumber=" + contractNo + "没有查询到数据");
			return result;
		}

		String loanStatus = tcmmLoanDetail.getLoanStatus();
		if (!CimsConstant.LOAN_WAY.equals(loanStatus)) {
			if (CimsConstant.LOAN_CONFIRM_WIN.equals(loanStatus) || CimsConstant.LOAN_CONFIRM_LOSE.equals(loanStatus)) {
				result.setErrCode(String.valueOf(ResponseDataEnum.EXISTS.getCode()));
			} else if (CimsConstant.LOAN_FUNDOUT_WIN.equals(loanStatus) || CimsConstant.LOAN_FUNDOUT_LOSE.equals(loanStatus)) {
				result.setErrMsg("借款信息已经放款");
			} else {
				result.setErrMsg("借款信息不符合审批条件");
			}
		} else {
			result.setErrMsg(null);
			result.setSuccess(true);
		}

		return result;
	}

	/**
	 * @description 验证贷款放款
	 * @param contractNo
	 *            借款信息审批编号
	 * @return JResult 错误信息
	 * @throws Exception
	 * @author shengzhoutao
	 * @date 2015-09-07
	 */
	public JResult validateLoanResultByContractNo(final String contractNo) throws Exception {
		JResult result = new JResult();
		result.setSuccess(false);
		result.setErrCode(String.valueOf(ResponseDataEnum.FAILURE.getCode()));
		TCMmLoanDetailDTO tcmmLoanDetail = getLoanDetailByContractNo(contractNo);
		if (null == tcmmLoanDetail) {
			result.setErrMsg("根据借款信息审批编号loanNumber=" + contractNo + "没有查询到数据");
			return result;
		}

		String loanStatus = tcmmLoanDetail.getLoanStatus();
		if (CimsConstant.LOAN_CONFIRM_WIN.equals(loanStatus) || CimsConstant.LOAN_FUNDOUT_LOSE.equals(loanStatus)) {
			result.setErrMsg(null);
			result.setSuccess(true);
		} else {
			if (CimsConstant.LOAN_FUNDOUT_WIN.equals(loanStatus)) {
				result.setErrCode(String.valueOf(ResponseDataEnum.EXISTS.getCode()));
			} else {
				result.setErrMsg("借款信息不符合放款条件");
			}
		}
		return result;
	}

	/**
	 * @description 根据募集计划编号查询 募集计划申请
	 * @param planId
	 *            募集计划编号
	 * @return 募集计划申请实体对象
	 * @throws Exception
	 * @author shengzhoutao
	 * @date 2015-09-07
	 */
	public TCMmRaisePlanDTO getRaisePlanByPlanId(final String planId) throws Exception {
		TCMmRaisePlanDTO tcmmRaisePlanDTO = new TCMmRaisePlanDTO();
		tcmmRaisePlanDTO.setCooperorgCode(CimsConstant.COOPERORG_CODE_TBJ);
		tcmmRaisePlanDTO.setPlanId(planId);
		List<TCMmRaisePlanDTO> tcmmRaisePlanDTOList = tcmmRaisePlanService.searchTCMmRaisePlan(SearchParamUtil.getSearchParam(tcmmRaisePlanDTO));
		tcmmRaisePlanDTO = null;
		if (CollectionsUtils.isEmpty(tcmmRaisePlanDTOList)) {
			return null;
		}
		return tcmmRaisePlanDTOList.get(0);
	}

	/**
	 * @description 根据产品编号查询产品相关的债权信息
	 * @param productId
	 *            产品编号
	 * @return 产品相关的债权信息
	 * @throws Exception
	 * @author shengzhoutao
	 * @date 2015-09-07
	 */
	public TCMmTbjLoanProdDTO getLoanProdByProductId(final String productId) throws Exception {
		TCMmTbjLoanProdDTO tcmmTbjLoanProdDTO = new TCMmTbjLoanProdDTO();
		tcmmTbjLoanProdDTO.setProductId(productId);
		List<TCMmTbjLoanProdDTO> tcmmTbjLoanProdDTOList = tcmmTbjLoanProdService.searchTCMmTbjLoanProd(SearchParamUtil
				.getSearchParam(tcmmTbjLoanProdDTO));
		tcmmTbjLoanProdDTO = null;
		if (CollectionsUtils.isEmpty(tcmmTbjLoanProdDTOList)) {
			return null;
		}

		return tcmmTbjLoanProdDTOList.get(0);
	}

	/**
	 * @description 根据借款信息审批编号查询 贷款明细
	 * @param contractNo
	 *            借款信息审批编号
	 * @return TCMmLoanDetailDTO 贷款明细
	 * @throws Exception
	 * @author shengzhoutao
	 * @date 2015-09-07
	 */
	public TCMmLoanDetailDTO getLoanDetailByContractNo(final String contractNo) throws Exception {
		TCMmLoanDetailDTO tcmmLoanDetail = new TCMmLoanDetailDTO();
		tcmmLoanDetail.setContractNo(contractNo);
		List<TCMmLoanDetailDTO> tcmmLoanDetailList = tcmmLoanDetailService.searchTCMmLoanDetail(SearchParamUtil.getSearchParam(tcmmLoanDetail));
		tcmmLoanDetail = null;
		if (CollectionsUtils.isEmpty(tcmmLoanDetailList)) {
			return null;
		}

		return tcmmLoanDetailList.get(0);
	}

	/**
	 * @description 贷款审批
	 * @param auditResultDtoList
	 *            审核结果查询:loanResult和loanNumber字段的值必须有。
	 * @throws Exception
	 * @author lizhenhuan
	 * @date 2015-09-21
	 */
	public void updateAuditResult(final List<AuditResultDto> auditResultDtoList) throws Exception {
		if (CollectionsUtils.isEmpty(auditResultDtoList)) {
			return;
		}

		for (AuditResultDto auditResultDto : auditResultDtoList) {
			if (null == auditResultDto) {
				continue;
			}

			int result = auditResultDto.getResult();
			String contractNo = auditResultDto.getLoanNumber();
			if (CimsConstant.TBJ_AUDIT_RESULT_SUCCESS.equals(String.valueOf(result))) {// 审核通过
				TCMmLoanDetailDTO dto = tcmmLoanDetailService.queryTCMmLoanDetailByContractNo(contractNo);
				String loanStatus = dto.getLoanStatus();
				if (CimsConstant.LOAN_WAY.equals(loanStatus)) {
					auditLoan(contractNo, CimsConstant.LOAN_EACH_STATUS_END, CimsConstant.LOAN_CONFIRM_WIN,null);
				}
			} else if (CimsConstant.TBJ_AUDIT_RESULT_FAIL.equals(String.valueOf(result))
					|| CimsConstant.TBJ_AUDIT_RESULT_RETURN.equals(String.valueOf(result))) {// 审核未通过/审核退回
				auditLoan(contractNo, CimsConstant.LOAN_EACH_STATUS_END, CimsConstant.LOAN_CONFIRM_LOSE,null);
			}
		}
	}

	/**
	 * @description 贷款放款
	 * @param loanResultDtoList
	 *            放款结果查询:loanResult和loanNumber字段的值必须有。
	 * @throws Exception
	 * @author lizhenhuan
	 * @date 2015-09-21
	 */
	public void updateLoanResult(final List<LoanResultDto> loanResultDtoList) throws Exception {
		if (CollectionsUtils.isEmpty(loanResultDtoList)) {
			return;
		}

		for (LoanResultDto loanResultDto : loanResultDtoList) {
			if (null == loanResultDto) {
				continue;
			}

			String result = String.valueOf(loanResultDto.getLoanResult());
			String contractNo = loanResultDto.getLoanNumber();
			Date fundDate = loanResultDto.getLoanTime();
			if (CimsConstant.TBJ_LOAN_RESULT_SUCCESS.equals(result)) {// 放款成功
				loanFundOut(contractNo, CimsIrtUrlConstant.IRT_LOAN_FUNDOUT_SUCCESS,fundDate);
				auditLoan(contractNo, CimsConstant.LOAN_EACH_STATUS_CORE, CimsConstant.LOAN_FUNDOUT_WIN,fundDate);
				insertCreditByContractNo(contractNo, fundDate);
			} else if (CimsConstant.TBJ_LOAN_RESULT_FAIL.equals(result)) {// 放款失败
				loanFundOut(contractNo, CimsIrtUrlConstant.IRT_LOAN_FUNDOUT_FAIL,null);
				auditLoan(contractNo, CimsConstant.LOAN_EACH_STATUS_CORE, CimsConstant.LOAN_FUNDOUT_LOSE,null);
			}
		}
		
		//更新募集计划项目总额
		tCMmRaisePlanService.updateTCMmRaisePlanItemSum();
		
	}

}
